<?php

namespace App\Services;

use App\Commons\CommonConstant;
use App\Jobs\AdPostbackToMediaQueue;
use App\Models\FirstupAd;
use App\Models\Log\LogAdPostbackMedia;
use App\Models\Orders;
use App\Models\OrdersCallClient;
use App\Models\Users;

class AdPostbackToMediaService extends ServiceBase
{
    public function addPostback($convert_type, $firstup_ad_id, $mod_id, $plan_id, $media_docking_id)
    {
        $log = LogAdPostbackMedia::whereRaw("firstup_ad_id={$firstup_ad_id} AND convert_type={$convert_type} AND mod_id={$mod_id}")->first();
        if ($log) {
            return;
        }

        $log                   = new LogAdPostbackMedia;
        $log->create_time      = time();
        $log->convert_type     = $convert_type;
        $log->firstup_ad_id    = $firstup_ad_id;
        $log->mod_id           = $mod_id;
        $log->plan_id          = $plan_id;
        $log->media_docking_id = $media_docking_id;
        $log->save();

        AdPostbackToMediaQueue::dispatch($log->id)->onQueue('default');
    }

    // 激活匹配成功后进行回传。并将关联的注册和付费一起回传
    public function postbackFirstUp($firstup_ad)
    {
        $plan_id = $firstup_ad->plan_id;
        $media_docking_id = $firstup_ad->media_docking_id;

        // 激活回传
        $this->addPostback(CommonConstant::EVENT_FIRSTUP, $firstup_ad->id, $firstup_ad->firstup_id, $plan_id, $media_docking_id);

        // 注册回传
        $users = Users::where('firstup_id', $firstup_ad->firstup_id)->get();
        foreach ($users as $val) {
            $this->addPostback(CommonConstant::EVENT_REG, $firstup_ad->id, $val->user_id, $plan_id, $media_docking_id);

            // 付费回传
            $orders = Orders::where('user_id', $val->user_id)->whereIn('status', [1,2,3,201])->get();
            foreach ($orders as $order) {
                $this->addPostback(CommonConstant::EVENT_PAY, $firstup_ad->id, $order->id, $plan_id, $media_docking_id);
            }
        }
    }

    // 注册成功回传
    public function postbackReg($user)
    {
        if ($user->firstup_id <= 0) {
            return;
        }
        $firstup_ad = FirstupAd::where('firstup_id', $user->firstup_id)->first();
        if (!$firstup_ad || $firstup_ad->monitor_id <= 0) {
            return;
        }

        $this->addPostback(CommonConstant::EVENT_REG, $firstup_ad->id, $user->user_id, $firstup_ad->plan_id, $firstup_ad->media_docking_id);
    }

    // 支付成功回传
    public function postbackPay($order)
    {
        $user = Users::find($order->user_id);
        if ($user->firstup_id <= 0) {
            return;
        }
        $firstup_ad = FirstupAd::where('firstup_id', $user->firstup_id)->first();
        if (!$firstup_ad || $firstup_ad->monitor_id <= 0) {
            $call = OrdersCallClient::where('order_id', $order->id)->first();
            if (!$call) {
                $call              = new OrdersCallClient;
                $call->create_time = time();
                $call->user_id     = $order->user_id;
                $call->order_id    = $order->id;
                $call->call_amount = $order->money;
                $call->save();
            }

            return;
        }

        $this->addPostback(CommonConstant::EVENT_PAY, $firstup_ad->id, $order->id, $firstup_ad->plan_id, $firstup_ad->media_docking_id);
    }
}
